<?php
/**
 * @copyright   Copyright (c) 2017 https://www.sapixx.com All rights reserved.
 * @license     Licensed (http://www.apache.org/licenses/LICENSE-2.0).
 * @author      pillar<ltmn@qq.com>
 * 商店管理
 */
namespace app\ais\controller;
use app\ais\model\AisQueen;
use app\ais\controller\Common;
use filter\Filter;
use think\facade\Request;

class Queen extends Common{

    public function initialize(){
        parent::initialize();
        $this->assign('pathMaps',[['name'=>'分帐列表','url'=>url("ais/queen/index")]]);
    }

    /**
     * 微信分账队列
     * @return void
     */
    public function index(int $types = 0,string $order = ''){
        $condition = [];
        if(!empty($order)){
            $condition['out_order_no'] = Filter::filter_escape(trim($order));
        }
        $store_id  = Request::param('store_id',0);
        $store_name  = Request::param('store_name');
        if($store_id){
            $condition['store_id'] = $store_id;
        }
        $view['amount_quree']   = AisQueen::where($this->mini_program)->where($condition)->where(['is_finish' => 0])->sum('amount');
        $view['amount_success'] = AisQueen::where($this->mini_program)->where($condition)->where(['is_finish' => 2])->sum('amount');
        $view['amount_fail']    = AisQueen::where($this->mini_program)->where($condition)->where(['is_finish' => 1])->sum('amount');
        if($types){
            switch ($types) {
                case 2:
                    $is_finish = 2;
                    break;
                case 3:
                    $is_finish = 1;
                    break;
                default:
                    $is_finish = 0;
                    break;
            }
            $condition['is_finish'] = $is_finish;
        }
        $orderby = $types == 1 ? 'id asc':'id desc';
        $view['lists'] = AisQueen::where($this->mini_program)->where($condition)->order($orderby)->paginate(10,false,[
            'query' => ['types' => $types,'store_id' => $store_id,'store_name' => $store_name],
        ]);
        $view['types'] = $types;
        $view['order'] = $order;
        $view['store_id']   = $store_id;
        $view['store_name'] = $store_name;
        $view['pathMaps'] = [['name'=>'结算队列','url'=>url("store/queen")]];
        return view()->assign($view);
    }


    /**
     * 重置分账队列
     * @return void
     */
    public function resetQueen(string $order){
        $condition = [];
        $condition['out_order_no'] = Filter::filter_escape(trim($order));
        $condition['is_finish']    = 1;
        $rueen = AisQueen::where($this->mini_program)->where($condition)->update(['is_finish' => 0]);
        if($rueen){
            return json(['code'=>200,'message'=>'成功']);
        }else{
            return json(['code'=>0,'message'=>'失败,只有分账失败的才允许重置队列']);
        }
    }
}